home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_09 / harmon / fmext.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-31  |  9.5 KB  |  325 lines

  1. Listing 1:
  2.  
  3. /******************************************************
  4. *  FMEXT.C - Generic File Manager Extension           *
  5. *  Copyright (c) 1994                                 *
  6. *  Trevor Harmon, Microdyne Development Technologies  *
  7. ******************************************************/
  8.  
  9. /* Use strict type casting for safety. */
  10. #define STRICT
  11.  
  12. /* Standard Windows definitions */
  13. #include <windows.h> 
  14. /* File Manager Extensions header */
  15. #include <wfext.h>   
  16. /* Local program definitions */
  17. #include "fmext.h"    
  18.  
  19. /* Function prototypes */
  20. void FillSelectionsListBox(HWND);
  21. void FillDriveInfoDlg(HWND);
  22.  
  23. /* Global constants */
  24. char szExtensionName[] =
  25.     "Generic File Manager Extension";
  26. char szMenuName[] = "&Extension";
  27.  
  28. /* Global variables */
  29. HINSTANCE hInst;    /* Handle to instance of DLL */
  30. WORD wFMMenuDelta;  /* Menu-item delta value */
  31. HWND hwndFileMan;   /* Handle to File Manager */
  32. /* Tells if the Checkmark Test menu item is checked */
  33. BOOL fItemChecked;
  34.  
  35. /* Starting point for the DLL */
  36. int FAR PASCAL LibMain(HINSTANCE hInstance,
  37.      WORD wDataSeg, WORD cbHeapSize, LPSTR lpCmdLine)
  38. {
  39.   /* Save the hInstance handle for later. */
  40.   hInst = hInstance;
  41.  
  42.   /* The menu item is initially unchecked. */ 
  43.   fItemChecked = FALSE;
  44.  
  45.   /* Perform any DLL initialization routines here. */
  46.  
  47.   /* Return success. */
  48.   return 1;
  49. }
  50.  
  51. /* Exit point for the DLL */
  52. int FAR PASCAL WEP(int nParameter)
  53. {
  54.   /* Perform any DLL cleanup routines here. */
  55.  
  56.   /* Return success. */
  57.   return 1;
  58. }
  59.  
  60. /******************************************************
  61. * Fill the list box in the given dialog with the File *
  62. * Manager selections.                                 *
  63. ******************************************************/
  64. void FillSelectionsListBox(HWND hWnd)
  65. {
  66.   WORD wSelectedItems;    /* Number of selections */
  67.   FMS_GETFILESEL fmsgfs;  /* File information */
  68.  
  69.   /* Retrieve the number of selected items. */
  70.   wSelectedItems = (WORD) SendMessage(hwndFileMan,
  71.                                FM_GETSELCOUNT, 0, 0L);
  72.  
  73.   /* If one or more items are selected... */
  74.   if (wSelectedItems > 0) {
  75.  
  76.     /* Loop through each selected file, retrieve */ 
  77.     /* its name, and add it to the list box. */
  78.     while (wSelectedItems > 0) {
  79.  
  80.       wSelectedItems--;
  81.       /* Send a File Manager message to fill the */
  82.       /* FMS_GETFILESEL structure. */
  83.       SendMessage(hwndFileMan, FM_GETFILESEL,
  84.            wSelectedItems, (LPARAM) &fmsgfs);
  85.       /* Transfer the szName field in the structure */
  86.       /* over to the list box. */
  87.       SendDlgItemMessage(hWnd, IDC_SELLIST,
  88.            LB_ADDSTRING, 0, (LPARAM) fmsgfs.szName);
  89.  
  90.     }
  91.  
  92.   }
  93.   else {
  94.  
  95.     /* If no items are selected, inform the user */
  96.     /* and abort. */
  97.     MessageBox(hWnd, "Please select at least one " \
  98.        "file or directory from the list on the right.",
  99.        "Show Selections", MB_ICONSTOP | MB_OK);
  100.     EndDialog(hWnd, 0);
  101.  
  102.   }
  103.  
  104. }
  105.  
  106. /* Dialog box procedure for Show Selections */
  107. BOOL FAR PASCAL ShowSelDlgProc(HWND hWnd, WORD wMsg,
  108.                      WPARAM wParam, LPARAM lParam)
  109. {
  110.   switch (wMsg) {
  111.  
  112.     case WM_INITDIALOG:
  113.       /* Fill list box with File Manager selections. */
  114.       FillSelectionsListBox(hWnd);
  115.       break;
  116.  
  117.     case WM_COMMAND:
  118.       /* End the dialog if OK is pressed or the */
  119.       /* box is closed. */
  120.       switch (wParam) {
  121.     case IDOK:
  122.         case IDCANCEL:
  123.           EndDialog(hWnd, 0);
  124.       break;
  125.       }
  126.  
  127.   }
  128.  
  129.   return FALSE;
  130. }
  131.  
  132. /******************************************************
  133. * Query the File Manager for information on the       *
  134. * current drive, then fill the dialog's static text   *
  135. * controls accordingly.                               *
  136. ******************************************************/
  137. void FillDriveInfoDlg(HWND hWnd)
  138. {
  139.   FMS_GETDRIVEINFO fmsgdi;  /* Info on current drive */
  140.   char s[10]; /* Buffer for number-string conversion */
  141.  
  142.   /* Retrieve info on current File Manager drive. */
  143.   SendMessage(hwndFileMan, FM_GETDRIVEINFO, 0,
  144.        (LPARAM) &fmsgdi);
  145.  
  146.   /* Convert the dwTotalSpace field to a string. */
  147.   wsprintf(s, "%lu", fmsgdi.dwTotalSpace);
  148.   /* Set the dialog's Total Space text control. */
  149.   SendDlgItemMessage(hWnd, IDC_TOTALSPACE, WM_SETTEXT,
  150.        0, (LPARAM) s);
  151.  
  152.   /* Convert the dwFreeSpace field to a string. */
  153.   wsprintf(s, "%lu", fmsgdi.dwFreeSpace);
  154.   /* Set the dialog's Free Space text control. */
  155.   SendDlgItemMessage(hWnd, IDC_FREESPACE, WM_SETTEXT,
  156.        0, (LPARAM) s);
  157.  
  158.   /* Set the dialog's Current Path text control. */
  159.   SendDlgItemMessage(hWnd, IDC_CURRENTPATH, WM_SETTEXT,
  160.        0, (LPARAM) fmsgdi.szPath);
  161.   /* Set the dialog's Volume Label text control. */
  162.   SendDlgItemMessage(hWnd, IDC_VOLUMELABEL, WM_SETTEXT,
  163.        0, (LPARAM) fmsgdi.szVolume);
  164.  
  165.   if ( lstrlen(fmsgdi.szShare) > 0 )
  166.     /* If the network sharepoint exists, set the */
  167.     /* dialog's text control. */
  168.     SendDlgItemMessage(hWnd, IDC_SHAREPOINT,
  169.          WM_SETTEXT, 0, (LPARAM) fmsgdi.szShare);
  170.   else
  171.     /* If the sharepoint is a null string, set the */
  172.     /* text control to "None". */
  173.     SendDlgItemMessage(hWnd, IDC_SHAREPOINT,
  174.          WM_SETTEXT, 0, (LPARAM) "None");
  175. }
  176.  
  177. /* Dialog box procedure for Drive Information */
  178. BOOL FAR PASCAL DriveInfoDlgProc(HWND hWnd, WORD wMsg,
  179.       WPARAM wParam, LPARAM lParam)
  180. {
  181.   switch (wMsg) {
  182.  
  183.     case WM_INITDIALOG:
  184.       /* Fill the dialog's text controls with */
  185.       /* information on the current drive. */
  186.       FillDriveInfoDlg(hWnd);
  187.       break;
  188.  
  189.     case WM_COMMAND:
  190.       /* End the dialog if OK is pressed or the */
  191.       /* dialog box is closed. */
  192.       switch (wParam) {
  193.     case IDOK:
  194.         case IDCANCEL:
  195.           EndDialog(hWnd, 0);
  196.       break;
  197.       }
  198.  
  199.   }
  200.  
  201.   return FALSE;
  202. }
  203.  
  204. /******************************************************
  205. * Entry point for FMEVENT_xxxx messages sent by the   *
  206. * File Manager to the Extension.                      *
  207. ******************************************************/
  208. HMENU FAR PASCAL FMExtensionProc(HWND hWnd, WORD wMsg,
  209.                       LONG lParam)
  210. {
  211.   FARPROC lpDlgProcInst; /* Points to a dlg function */
  212.   HMENU hMenu;  /* Handle to a menu */
  213.  
  214.   /* Store the File Manager's window handle. */
  215.   hwndFileMan = hWnd;
  216.  
  217.   switch (wMsg) {
  218.  
  219.     case FMEVENT_LOAD:
  220.     {
  221.       LPFMS_LOAD lpfmsl; /* Holds info on Extension */
  222.  
  223.       /* Convert the lParam parameter to an FMS_LOAD */
  224.       /* pointer to make reading the code easier. */
  225.       lpfmsl = (LPFMS_LOAD) lParam;
  226.  
  227.       /* Initialize the FMS_LOAD structure... */
  228.       /* Size of structure */
  229.       lpfmsl->dwSize = sizeof(FMS_LOAD);     
  230.       /* Name of Extension menu */
  231.       lstrcpy(lpfmsl->szMenuName, szMenuName);
  232.       /* Load Extension menu from .RC into memory. */
  233.       hMenu = LoadMenu(hInst, "FMExtMenu");     
  234.       /* Store Extension menu. */
  235.       lpfmsl->hMenu = hMenu;               
  236.  
  237.       /* Get the File Manager's menu-item delta for */
  238.       /* this Extension. */
  239.       wFMMenuDelta = lpfmsl->wMenuDelta;
  240.  
  241.       /* Important: non-zero value must be returned. */
  242.       return hMenu;
  243.     }
  244.  
  245.     case FMEVENT_INITMENU:
  246.       /* Convert the lParam parameter to an hMenu */
  247.       /* handle to make reading the code easier. */
  248.       hMenu = (HMENU) HIWORD(lParam);
  249.       /* If the Checkmark Test menu item is checked, */
  250.       /* place a checkmark on the item.  Otherwise, */
  251.       /* remove the checkmark. */
  252.       if (fItemChecked)
  253.         CheckMenuItem(hMenu,
  254.              wFMMenuDelta + IDM_INITMENUTEST,
  255.              MF_CHECKED | MF_BYCOMMAND);
  256.       else
  257.         CheckMenuItem(hMenu,
  258.              wFMMenuDelta + IDM_INITMENUTEST,
  259.              MF_UNCHECKED | MF_BYCOMMAND);
  260.       break;
  261.  
  262.     case FMEVENT_SELCHANGE:
  263.       break;
  264.  
  265.     case FMEVENT_USER_REFRESH:
  266.       MessageBox(hWnd, "The user refreshed the " \
  267.            "File Manager windows.", "User Refresh",
  268.            MB_OK | MB_ICONINFORMATION);
  269.       break;
  270.  
  271.     case FMEVENT_UNLOAD:
  272.       break;
  273.  
  274.     case IDM_SHOWSEL:
  275.       /* Display the Show Selections dialog box. */
  276.       lpDlgProcInst = MakeProcInstance( (FARPROC)
  277.            ShowSelDlgProc, hInst);
  278.       DialogBox(hInst, "ShowSel", hwndFileMan,
  279.            (DLGPROC) lpDlgProcInst);
  280.       FreeProcInstance(lpDlgProcInst);
  281.       break;
  282.  
  283.     case IDM_DRIVEINFO:
  284.       /* Display the Drive Information dialog box. */
  285.       lpDlgProcInst = MakeProcInstance( (FARPROC)
  286.            DriveInfoDlgProc, hInst);
  287.       DialogBox(hInst, "DriveInfo", hwndFileMan,
  288.            (DLGPROC) lpDlgProcInst);
  289.       FreeProcInstance(lpDlgProcInst);
  290.       break;
  291.  
  292.     case IDM_INITMENUTEST:
  293.       /* Invert the global variable fItemChecked. */
  294.       fItemChecked = ~fItemChecked;
  295.       break;
  296.  
  297.     case IDM_REFRESH:
  298.       /* Tell File Manager to refresh its windows. */
  299.       SendMessage(hwndFileMan, FM_REFRESH_WINDOWS,
  300.            TRUE, 0);
  301.       break;
  302.  
  303.     case IDM_UNINSTALL:
  304.       /* Check to see if the user really wants to */
  305.       /* remove the Extension. */ 
  306.       if ( MessageBox(hWnd, "Are you sure you want " \
  307.         "to remove the extension?", "Remove Extension",
  308.         MB_YESNO | MB_ICONQUESTION) == IDYES ) {
  309.  
  310.         /* Remove Extension string from WINFILE.INI. */
  311.     WritePrivateProfileString("AddOns",
  312.              szExtensionName, "", "winfile.ini");
  313.         /* Tell File Manager to reload Extensions. */
  314.     PostMessage(hwndFileMan, FM_RELOAD_EXTENSIONS,
  315.              0, 0L);
  316.  
  317.       }
  318.       break;
  319.  
  320.   }
  321.  
  322.   return (HMENU) 0;
  323. }
  324.  
  325.